package com.vader.admin.service.impl;

import com.vader.admin.client.DistributeIdClient;
import com.vader.admin.dao.BaseTaskRecordDao;
import com.vader.admin.entity.DO.BaseTaskRecord;
import com.vader.admin.service.BaseTaskRecordService;
import com.vader.common.entity.CommonResponse;
import com.vader.common.exception.BadRequestException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 活动参加记录表(BaseTaskRecord)表服务实现类
 *
 * @author Vader
 * @since 2021-10-06 12:46:39
 */
@Service("baseTaskRecordService")
public class BaseTaskRecordServiceImpl implements BaseTaskRecordService {
    @Resource
    private BaseTaskRecordDao baseTaskRecordDao;
    @Resource
    private DistributeIdClient distributeIdClient;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BaseTaskRecord queryById(Long id) {
        return this.baseTaskRecordDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param baseTaskRecord 筛选条件
     * @param pageRequest    分页对象
     * @return 查询结果
     */
    @Override
    public Page<BaseTaskRecord> queryByPage(BaseTaskRecord baseTaskRecord, PageRequest pageRequest) {
        long total = this.baseTaskRecordDao.count(baseTaskRecord);
        return new PageImpl<>(this.baseTaskRecordDao.queryAllByLimit(baseTaskRecord, pageRequest), pageRequest, total);
    }

    /**
     * 用户报名
     *
     * @param baseTaskRecord 实例对象
     * @return 实例对象
     */
    @Override
    public BaseTaskRecord insert(BaseTaskRecord baseTaskRecord) {
        CommonResponse<Long> id = distributeIdClient.getId();
        if (id.getCode()!=0){
            throw new BadRequestException("用户互动记录id生成异常");
        }
        baseTaskRecord.setTaskId(id.getData());
        baseTaskRecord.setCreateAt(System.currentTimeMillis());
        baseTaskRecord.setUpdateAt(System.currentTimeMillis());
        this.baseTaskRecordDao.insert(baseTaskRecord);
        return baseTaskRecord;
    }

    /**
     * 修改数据
     *
     * @param baseTaskRecord 实例对象
     * @return 实例对象
     */
    @Override
    public BaseTaskRecord update(BaseTaskRecord baseTaskRecord) {
        this.baseTaskRecordDao.update(baseTaskRecord);
        return this.queryById(baseTaskRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.baseTaskRecordDao.deleteById(id) > 0;
    }
}
